package com.lagou.order.service.impl;

import com.lagou.order.dao.GoodDao;
import com.lagou.order.dao.OrderDao;
import com.lagou.order.pojo.Good;
import com.lagou.order.pojo.Order;
import com.lagou.order.service.GoodService;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

/**
 * @author stevejson
 */
@Service(value = "goodService")
public class GoodServiceImpl implements GoodService {
    private static final String KEY_PRE = "good:";

    @Resource
    private GoodDao goodDao;

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @Resource
    private OrderDao orderDao;

    @Resource
    private RocketMQTemplate rocketMQTemplate;

    @Override
    public Integer getGood(Integer id) {
        System.out.println("获取商品"+id);
        String key = KEY_PRE + id;
        String value = redisTemplate.opsForValue().get(key);
        if(value != null){
            return Integer.valueOf(value);
        }else{
            // 查询数据库
            Optional<Good> good = goodDao.findById(id);
            if(good.isEmpty()){
                redisTemplate.opsForValue().set(key, "0", 5, TimeUnit.MINUTES);
                return 0;
            }else{
                redisTemplate.opsForValue().set(key, String.valueOf(good.get().getCount()), 5, TimeUnit.MINUTES);
                return good.get().getCount();
            }

        }
    }

    @Override
    public Boolean orderGood(Integer id) {
        System.out.println("创建订单"+id);
        String key = KEY_PRE + id;

        String value = redisTemplate.opsForValue().get(key);
        if(value == null || Integer.parseInt(value) <= 0){
            return false;
        }
        redisTemplate.opsForValue().decrement(key);
        goodDao.decrCount(id);
        // 创建数据库
        Order order = new Order();
        order.setOrderId(id);
        order.setVersion(0);
        order.setState(Order.STATE.CREATE);
        order.setName("phone");
        orderDao.save(order);
        // 发送mq
        System.out.println("发送mq"+order.getId());
        rocketMQTemplate.syncSend("topic-order:delay", MessageBuilder.withPayload(order.getId()).build(), 10_000, 4);
        return true;
    }

    @Override
    public Boolean payGood(Integer id) {
        System.out.println("支付订单"+id);
        Order order = orderDao.findById(id).get();
        System.out.println(order);
        if(!Order.STATE.CREATE.equals(order.getState())){
            return false;
        }
        Integer integer = orderDao.updateOrderState(order.getVersion(), id, Order.STATE.PAYED.ordinal());
        return integer == 1;
    }

    @Override
    public Boolean cancelGood(Integer id) {
        System.out.println("取消订单"+id);

        Optional<Order> or = orderDao.findById(id);
        if (or.isEmpty()) {
            return false;
        }
        Order order = or.get();
        if(!Order.STATE.CREATE.equals(order.getState())){
            return false;
        }
        Integer integer = orderDao.updateOrderState(order.getVersion(), id, Order.STATE.CANCEL.ordinal());
        Optional<Good> good = goodDao.findById(order.getOrderId());
        if(good.isEmpty()){
            return true;
        }
        goodDao.incrCount(good.get().getId());
        String key = KEY_PRE + order.getOrderId();
        redisTemplate.opsForValue().increment(key);
        return integer == 1;
    }
}
